<?php
/**
 * @yiwan
 * 最后关联wiki的uid
 * 最后关联wiki分类的uid
 */

use Biz\cache\wiki\LastRelateUids;
use Biz\cache\wiki\LastRelateUidsOfCategory;
use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

$num = 100000;
$process = $argv[1] ?? 1;

$min = ($process - 1) * $num;
$max = $min + $num;

// 同步ObjRelateWiki表的数据至es
$page = 1;
$pageSize = 2000;
do {
    $startId = $min + (($page - 1) * $pageSize);
    $endId = $startId + $pageSize;
    $list = mObjRelateWiki::select('*', [
        'status' => mObjRelateWiki::STATUS_NORMAL,
        'id[>=]' => $startId,
        'id[<]' => $endId,
    ]);
    foreach ($list as $value) {
        $wikiId = $value['wiki_id'];
        $uid = $value['uid'];
        $time = strtotime($value['addtime']);
        if ($wikiId > 0 && $uid > 0) {
            var_dump('id:' . $value['id'] . '#wiki_id:' . $wikiId . '#uid:' . $uid);
            $wikiInfo = mWiki::getInfoByWikiId($wikiId, ['id', 'category_id', 'second_category_id', 'third_category_id', 'four_category_id']);
            if ($wikiInfo) {
                LastRelateUids::addLastUid($wikiId, $uid, $time);
                if ($wikiInfo['category_id'] > 0) {
                    LastRelateUidsOfCategory::addLastUid($wikiInfo['category_id'], $uid, $time);
                }
                if ($wikiInfo['second_category_id'] > 0) {
                    LastRelateUidsOfCategory::addLastUid($wikiInfo['second_category_id'], $uid, $time);
                }
                if ($wikiInfo['third_category_id'] > 0) {
                    LastRelateUidsOfCategory::addLastUid($wikiInfo['third_category_id'], $uid, $time);
                }
                if ($wikiInfo['four_category_id'] > 0) {
                    LastRelateUidsOfCategory::addLastUid($wikiInfo['four_category_id'], $uid, $time);
                }
            }
        }
    }
    $page++;
} while ($endId < $max);


